package com.board.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.text.split.SplitIter;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.board.client.CanteenServiceClient;
import com.board.client.FoodServiceClient;
import com.board.client.StoreServiceClient;
import com.board.client.UserServiceClient;
import com.board.client.dto.CanteenDto;
import com.board.client.dto.FoodDto;
import com.board.client.dto.StoreDto;
import com.board.client.dto.UserDto;
import com.board.config.AlipayConfiguration;
import com.board.dao.OrderDetailMapper;
import com.board.dao.OrderLogMapper;
import com.board.dao.SeatEatOrderMapper;
import com.board.dao.SendOrderMapper;
import com.board.dto.*;
import com.board.entity.OrderDetail;
import com.board.entity.OrderLog;
import com.board.entity.SeatEatOrder;
import com.board.entity.SendOrder;
import com.board.exception.SeatOrderException;
import com.board.form.*;
import com.board.param.*;
import com.board.service.SeatEatOrderService;
import com.board.service.SendOrderService;
import com.board.util.*;
import io.swagger.models.auth.In;
import jdk.nashorn.internal.objects.Global;
import org.apache.ibatis.annotations.Param;
import org.bouncycastle.asn1.esf.OtherRevRefs;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.MediaSize;
import javax.xml.bind.annotation.XmlElementDecl;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 膳食系统-堂食订单表 服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2024年04月04日
 */
@Service
public class SeatEatOrderServiceImpl extends ServiceImpl<SeatEatOrderMapper, SeatEatOrder> implements SeatEatOrderService {
    @Autowired
    private SeatEatOrderMapper seatEatOrderMapper;
    @Autowired
    private CanteenServiceClient canteenServiceClient;
    @Autowired
    private StoreServiceClient storeServiceClient;
    @Autowired
    private FoodServiceClient foodServiceClient;
    @Autowired
    private UserServiceClient userServiceClient;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private AlipayUtil alipayUtil;
    @Autowired
    private AlipayConfiguration alipayConfiguration;
    @Autowired
    private SendOrderMapper sendOrderMapper;
    @Autowired
    private SendOrderService sendOrderService;
    @Autowired
    private OrderLogMapper orderLogMapper;

    @Override
    public Object createOrder(SeatOrderParam param, Integer userId) {
        //下订单的用户要通过实名认证
        Result<UserDto> result1 = userServiceClient.findUserById(userId);
        if (result1.getCode()!=200){
            throw new SeatOrderException(result1.getCode(),result1.getMessage());
        }
        System.out.println(result1.getData());
        if (result1.getData().getIdState()==StateDate.ID_STATE_NOT) throw new SeatOrderException(500,"用户未实名认证");
        //调用食堂服务查询食堂id是否存在及状态要正常
        Result<CanteenDto> result = canteenServiceClient.findCanteenById(param.getCanteenId());
        if (result.getCode()!=200){
            throw new SeatOrderException(500,"食堂不存在");
        }
        CanteenDto canteenDto = result.getData();
        System.out.println(canteenDto);
        if (canteenDto.getCanteenState()!=0){
            throw new SeatOrderException(500,"食堂状态异常");
        }
        //调用商家服务查询商家id是否存在及状态要正常
        Result<StoreDto> storeResult = storeServiceClient.findStoreById(param.getStoreId());
        if (storeResult.getCode()!=200){
            throw new SeatOrderException(500,"商家不存在");
        }
        StoreDto storeDto = storeResult.getData();
        if (storeDto.getStoreState()!=3){
            throw new SeatOrderException(500,"商家状态异常");
        }
        //传递进来的菜品id不能有重复的
        List<Integer> foodIds = param.getFoodDtoList().stream().map(o -> o.getFoodId()).collect(Collectors.toList());
        HashSet<Integer> set = new HashSet<>(foodIds);

        if (set.size()<foodIds.size()){
            throw new SeatOrderException(500,"菜品重复");
        }
        //调用菜品服务查询订单的所有菜品是否存在及状态要正常
        Result<List<FoodDto>> foodByIds = foodServiceClient.findFoodByIds(foodIds);
        if (foodByIds.getCode()!=200){
            throw new SeatOrderException(500,foodByIds.getMessage());
        }
        if (foodByIds.getData().size()!=foodIds.size()){
            throw new SeatOrderException(500,"有菜品不存在");
        }
        List<FoodDto> foodDtos = foodByIds.getData();
        BigDecimal totalPrice = new BigDecimal(0);
        HashMap<Integer, FoodDto> foodDtoHashMap = new HashMap<>();
        for (FoodDto foodDto : foodDtos) {
            if (foodDto.getFoodState()!=0){
                throw new SeatOrderException(500,"有菜品状态异常");
            }
            //计算总价
            for (FoodOrderForm orderForm : param.getFoodDtoList()) {
                if (foodDto.getFoodId().equals(orderForm.getFoodId())){
                    totalPrice = totalPrice.add(foodDto.getFoodPrice().multiply(new BigDecimal(orderForm.getFoodNum())));
                    break;
                }
            }
            //设置到map里面去
            foodDtoHashMap.put(foodDto.getFoodId(),foodDto);
        }
        //调用用户服务查询 下订单的用户信息
        Result<UserDto> userDtoResult = userServiceClient.findUserById(userId);
        if (userDtoResult.getCode()!=200){
            throw new SeatOrderException(500,"用户不存在");
        }
        //用户需要实名认证才能下订单
        UserDto userDto = userDtoResult.getData();
//        if (userDto.getIdState()!=0){
//            throw new SeatOrderException(500,"用户未实名认证");
//        }
        //可以进行新增订单
        System.out.println(param);
        System.out.println(param.getOrderType());
        if (param.getOrderType()==0){
            //堂食订单
            SeatEatOrder eatOrder = new SeatEatOrder();
            String orderNum = new Snowflake(1, 1).nextIdStr();
            eatOrder.setOrderNum(orderNum);
            eatOrder.setUserId(userId);
            eatOrder.setOrderUser(userDto.getUserName());
            eatOrder.setCreateTime(LocalDateTime.now().toString());
            eatOrder.setRemarks(param.getRemarks());
            eatOrder.setTotalPrice(totalPrice);
            eatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_NOPAY);
            eatOrder.setOrderType(StateDate.ORDER_TYPE_SEAT);
            seatEatOrderMapper.insert(eatOrder);
            //生成订单详情
            ArrayList<FoodOrderDto> foodOrderDtos = new ArrayList<>(); //返回的数据
            for (FoodOrderForm foodOrderForm : param.getFoodDtoList()) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOrderNum(orderNum);
                orderDetail.setCanteenName(canteenDto.getCanteenName());
                orderDetail.setFoodName(foodDtoHashMap.get(foodOrderForm.getFoodId()).getFoodName());
                orderDetail.setFoodPrice(foodDtoHashMap.get(foodOrderForm.getFoodId()).getFoodPrice());
                orderDetail.setFoodNum(foodOrderForm.getFoodNum());
                orderDetail.setStoreName(storeDto.getStoreName());
                orderDetail.setStoreId(storeDto.getStoreId());
                orderDetail.setOrderType(StateDate.ORDER_TYPE_SEAT);
                orderDetail.setCanteenId(param.getCanteenId());
                orderDetail.setFoodId(foodOrderForm.getFoodId());
                orderDetailMapper.insert(orderDetail);

                FoodOrderDto foodOrderDto = new FoodOrderDto();
                foodOrderDto.setFoodName(foodDtoHashMap.get(foodOrderForm.getFoodId()).getFoodName());
                foodOrderDto.setFoodPrice(foodDtoHashMap.get(foodOrderForm.getFoodId()).getFoodPrice());
                foodOrderDto.setFoodNum(foodOrderForm.getFoodNum());
                foodOrderDtos.add(foodOrderDto);
            }
            //往延迟队列中存储订单编号验证用户是否支付
            rabbitTemplate.convertAndSend("seat_order_dead_queue", orderNum);
            //往redis中存储剩余支付时间
            LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(15);
            long time = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();//15分钟后的时间戳
            redisUtil.setString(RedisKey.seatOrderPayTime(orderNum),time+"",60*15);
            //生成订单日志数据
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(param.getOrderType());
            orderLog.setOrderNum(orderNum);
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(userDto.getUserName());
            orderLog.setLogDesc(userDto.getUserName()+"生成堂食订单");
            orderLogMapper.insert(orderLog);
            //返回数据
            SeatOrderDto dto = BeanUtil.toBean(eatOrder, SeatOrderDto.class);
            dto.setOrderStateName("未支付");
            dto.setCanteenName(canteenDto.getCanteenName());
            dto.setStoreName(storeDto.getStoreName());
            dto.setFoodOrderList(foodOrderDtos);
            return dto;
        }else{
            //外送订单
            SendOrder sendOrder = new SendOrder();
            String orderNum = new Snowflake(1, 1).nextIdStr();
            sendOrder.setOrderNum(orderNum);
            sendOrder.setUserId(userId);
            sendOrder.setUserName(userDto.getUserName());
            sendOrder.setCreateTime(LocalDateTime.now().toString());
            sendOrder.setOrderRemark(param.getRemarks());
            sendOrder.setOrderPrice(totalPrice);
            sendOrder.setOrderState(StateDate.SEND_ORDER_STATE_NOPAY);
            sendOrder.setOrderType(StateDate.ORDER_TYPE_SEND);
            sendOrder.setAddressName(param.getAddressName());
            sendOrder.setAddressInfo(param.getAddressInfo());
            sendOrder.setAddressPhone(param.getAddressPhone());
            sendOrderMapper.insert(sendOrder);
            //生成订单详情
            ArrayList<FoodOrderDto> foodOrderDtos = new ArrayList<>(); //返回的数据
            for (FoodOrderForm foodOrderForm : param.getFoodDtoList()) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOrderNum(orderNum);
                orderDetail.setCanteenName(canteenDto.getCanteenName());
                orderDetail.setFoodName(foodDtoHashMap.get(foodOrderForm.getFoodId()).getFoodName());
                orderDetail.setFoodPrice(foodDtoHashMap.get(foodOrderForm.getFoodId()).getFoodPrice());
                orderDetail.setFoodNum(foodOrderForm.getFoodNum());
                orderDetail.setStoreName(storeDto.getStoreName());
                orderDetail.setStoreId(storeDto.getStoreId());
                orderDetail.setOrderType(StateDate.ORDER_TYPE_SEND);
                orderDetail.setCanteenId(param.getCanteenId());
                orderDetailMapper.insert(orderDetail);

                FoodOrderDto foodOrderDto = new FoodOrderDto();
                foodOrderDto.setFoodName(foodDtoHashMap.get(foodOrderForm.getFoodId()).getFoodName());
                foodOrderDto.setFoodPrice(foodDtoHashMap.get(foodOrderForm.getFoodId()).getFoodPrice());
                foodOrderDto.setFoodNum(foodOrderForm.getFoodNum());
                foodOrderDtos.add(foodOrderDto);
            }
            //往延迟队列中存储订单编号验证用户是否支付
            rabbitTemplate.convertAndSend("send_order_dead_queue", orderNum);
            //往redis中存储剩余支付时间
            LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(15);
            long time = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();//15分钟后的时间戳
            redisUtil.setString(RedisKey.sendOrderPayTime(orderNum),time+"",60*15);
            //生成订单日志数据
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(param.getOrderType());
            orderLog.setOrderNum(orderNum);
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(userDto.getUserName());
            orderLog.setLogDesc(userDto.getUserName()+"生成外送订单");
            orderLogMapper.insert(orderLog);
            //返回数据
            SendOrderDto dto = BeanUtil.toBean(sendOrder, SendOrderDto.class);
            dto.setOrderStateName("未支付");
            dto.setCanteenName(canteenDto.getCanteenName());
            dto.setStoreName(storeDto.getStoreName());
            dto.setFoodOrderList(foodOrderDtos);
            return dto;
        }


    }

    @Override
    public List<SeatOrderDto> userFindOrder(UserFindSeatOrderParam param, Integer userId) {
        QueryWrapper<SeatEatOrder> orderWrapper = new QueryWrapper<>();
        QueryWrapper<OrderDetail> detailWrapper = new QueryWrapper<>();
        orderWrapper.eq("user_id",userId);
        //先只从订单表里面查询,判断是否传递时间段
        if (param.getTimeChose()!=null){
            if (param.getTimeChose()!=0&&param.getTimeChose()!=1){
                throw new SeatOrderException(500,"时间段参数错误");
            }
            if (param.getTimeChose()==0){
                //一周内
                orderWrapper.apply("date_sub(curdate(),interval 7 day) <= date(create_time)");
            }else{
                //一个月内
                orderWrapper.apply("date_sub(curdate(),interval 1 month) <= date(create_time)");
            }
        }
        List<SeatEatOrder> seatEatOrders = seatEatOrderMapper.selectList(orderWrapper);
        //组装订单表查询到的数据转为map,方便后续使用
//        HashMap<String, SeatEatOrder> map = new HashMap<>();
//        for (SeatEatOrder seatEatOrder : seatEatOrders) {
//            map.put(seatEatOrder.getOrderNum(),seatEatOrder);
//        }
        //如果传递了食堂名称和商店名称，需要去详情表中查询，并且是在上面的订单编号中筛选
        if(param.getStoreName()!=null&&param.getStoreName()!=""){
            detailWrapper.like("store_name",param.getStoreName());
        }
        if (param.getCanteenName()!=null&&param.getCanteenName()!=""){
            detailWrapper.like("canteen_name",param.getCanteenName());
        }

        detailWrapper.in("order_num",seatEatOrders.stream().map(SeatEatOrder::getOrderNum).collect(Collectors.toList()));
        List<OrderDetail> details = orderDetailMapper.selectList(detailWrapper);
        //上面查询回来的details才是真正所有的订单
        ArrayList<SeatOrderDto> list = new ArrayList<>();
        HashMap<String, List<OrderDetail>> detailHashmap = new HashMap<>();
        for (OrderDetail detail : details) {
            if (!detailHashmap.containsKey(detail.getOrderNum())){
                ArrayList<OrderDetail> orderDetails = new ArrayList<>();
                orderDetails.add(detail);
                detailHashmap.put(detail.getOrderNum(),orderDetails);
            }else{
                List<OrderDetail> orderDetails = detailHashmap.get(detail.getOrderNum());
                orderDetails.add(detail);
                detailHashmap.put(detail.getOrderNum(),orderDetails);
            }
        }
        //循环订单表查询回来的数据，如果map中有这个订单编号，就添加到list中
        for (SeatEatOrder seatEatOrder : seatEatOrders) {
            if (detailHashmap.containsKey(seatEatOrder.getOrderNum())){
                SeatOrderDto dto = BeanUtil.toBean(seatEatOrder, SeatOrderDto.class);
                List<OrderDetail> detailList = detailHashmap.get(seatEatOrder.getOrderNum());
                dto.setCanteenName(detailList.get(0).getCanteenName());
                dto.setStoreName(detailList.get(0).getStoreName());
                dto.setFoodOrderList(BeanUtil.copyToList(detailList,FoodOrderDto.class));
                dto.setOrderStateName(seatEatOrder.getOrderState()==0?"待支付":
                        seatEatOrder.getOrderState()==1?"已取消":
                        seatEatOrder.getOrderState()==2?"待接单":
                        seatEatOrder.getOrderState()==3?"商家未接单":
                        seatEatOrder.getOrderState()==4?"已接单":
                        seatEatOrder.getOrderState()==5?"已出餐":
                        seatEatOrder.getOrderState()==6?"已取餐":"已退款");
                list.add(dto);
            }
        }
        return list;
    }

    @Override
    public Object cancelOrder(CancelOrderForm form, Integer userId) {
        //判断订单编号是否存在且是否是用户的
        if (form.getOrderType()==0){
            SeatEatOrder seatEatOrder=seatEatOrderMapper.selectById(form.getOrderNum());
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //订单的状态为未支付才能进行取消
            if (seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_NOPAY){
                throw new SeatOrderException(500,"订单状态不能取消");
            }
            //修改订单状态并返回数据
            seatEatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_USER_CANCEL);
            seatEatOrder.setCancelTime(LocalDateTime.now().toString());
            seatEatOrderMapper.updateById(seatEatOrder);
            SeatOrderDto dto = BeanUtil.toBean(seatEatOrder, SeatOrderDto.class);
            List<OrderDetail> details = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("order_num", form.getOrderNum()));
            if (details!=null){
                dto.setCanteenName(details.get(0).getCanteenName());
                dto.setStoreName(details.get(0).getStoreName());
                dto.setOrderStateName("已取消");
                dto.setFoodOrderList(BeanUtil.copyToList(details,FoodOrderDto.class));
            }
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(form.getOrderType());
            orderLog.setOrderNum(seatEatOrder.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"手动取消堂食订单");
            orderLogMapper.insert(orderLog);
            return dto;
        }else{
            SendOrder seatEatOrder=sendOrderMapper.selectById(form.getOrderNum());
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //订单的状态为未支付才能进行取消
            if (seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_NOPAY){
                throw new SeatOrderException(500,"订单状态不能取消");
            }
            //修改订单状态并返回数据
            seatEatOrder.setOrderState(StateDate.SEND_ORDER_STATE_USER_CANCEL);
            seatEatOrder.setCancelTime(LocalDateTime.now().toString());
            sendOrderMapper.updateById(seatEatOrder);
            SendOrderDto dto = BeanUtil.toBean(seatEatOrder, SendOrderDto.class);
            List<OrderDetail> details = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("order_num", form.getOrderNum()));
            if (details!=null){
                dto.setCanteenName(details.get(0).getCanteenName());
                dto.setStoreName(details.get(0).getStoreName());
                dto.setOrderStateName("已取消");
                dto.setFoodOrderList(BeanUtil.copyToList(details,FoodOrderDto.class));
            }
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(form.getOrderType());
            orderLog.setOrderNum(seatEatOrder.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"手动取消外送订单");
            orderLogMapper.insert(orderLog);
            return dto;
        }


    }

    @Override
    public String payOrder(PayOrderForm form, Integer userId) throws Exception {
        if (form.getOrderType()!=null){
            if (form.getOrderType()!=0&&form.getOrderType()!=1){
                throw new SeatOrderException(500,"订单类型错误");
            }
        }
        if (form.getOrderType()==0){
            //堂食订单
            //判断用户剩余支付时间是否足够
            if(!redisUtil.exists(RedisKey.seatOrderPayTime(form.getOrderNum()))){
                throw new SeatOrderException(500,"超出支付时间");
            }
            long time = Long.parseLong(redisUtil.getString(RedisKey.seatOrderPayTime(form.getOrderNum())));
            long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
            long second=(time-now)/1000; //剩余的秒数
            if(second<60){
                throw new SeatOrderException(500,"剩余支付时间太少，不足以完成支付");
            }
            //判断订单编号是否存在且是否是用户的
            SeatEatOrder seatEatOrder = seatEatOrderMapper.selectById(form.getOrderNum());
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //订单的状态为未支付才能进行支付
            if (seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_NOPAY){
                throw new SeatOrderException(500,"订单不是未支付状态");
            }
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(form.getOrderType());
            orderLog.setOrderNum(form.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"发起支付堂食订单");
            orderLogMapper.insert(orderLog);
            //组装支付参数
            OrderPayParam param = new OrderPayParam();
            param.setOrderId(form.getOrderNum());
            param.setMoney(seatEatOrder.getTotalPrice().doubleValue());
            param.setSubject("支付堂食订单");
            param.setBody("");
            param.setReturnUrl("http://www.woniuxy.com");
            param.setNotifyUrl("http://4nujqn.natappfree.cc/board-order/seatEatOrder/orderCallBack");
            param.setPayTimeOrder((int)second/60);
            String pay = alipayUtil.pagePay(param);
            return pay;
        }else{
            //外送订单
            //判断用户剩余支付时间是否足够
            if(!redisUtil.exists(RedisKey.sendOrderPayTime(form.getOrderNum()))){
                throw new SeatOrderException(500,"超出支付时间");
            }
            long time = Long.parseLong(redisUtil.getString(RedisKey.sendOrderPayTime(form.getOrderNum())));
            long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
            long second=(time-now)/1000; //剩余的秒数
            if(second<60){
                throw new SeatOrderException(500,"剩余支付时间太少，不足以完成支付");
            }
            //判断订单编号是否存在且是否是用户的
            SendOrder seatEatOrder = sendOrderMapper.selectById(form.getOrderNum());
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //订单的状态为未支付才能进行支付
            if (seatEatOrder.getOrderState()!=StateDate.SEND_ORDER_STATE_NOPAY){
                throw new SeatOrderException(500,"订单不是未支付状态");
            }
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(form.getOrderType());
            orderLog.setOrderNum(form.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"发起支付外送订单");
            orderLogMapper.insert(orderLog);
            //组装支付参数
            OrderPayParam param = new OrderPayParam();
            param.setOrderId(form.getOrderNum());
            param.setMoney(seatEatOrder.getOrderPrice().doubleValue());
            param.setSubject("支付外送订单");
            param.setBody("");
            param.setReturnUrl("http://www.woniuxy.com");
            param.setNotifyUrl("");
            param.setPayTimeOrder((int)second/60);
            String pay = alipayUtil.pagePay(param);
            return pay;
        }

    }

    @Override
    public void orderCallBack(Map<String, String> params) throws AlipayApiException {
        //验签
        boolean sign = AlipaySignature.rsaCheckV1(params, alipayConfiguration.getAlipayPublicKey(), "utf-8", "RSA2");//调用SDK验证前面
        if(!sign) throw new SeatOrderException(500,"验签失败");
        //验签通过，取出参数中的订单编码、订单状态、支付时间
        String orderId = params.get("out_trade_no");
        String tradeStatus = params.get("trade_status");
        String payTime = params.get("gmt_payment");
        if("TRADE_SUCCESS".equals(tradeStatus)){
            if (params.get("order_type").equals("0")){
                //堂食订单
                //用户已支付，判断订单是否未支付
                SeatEatOrder seatEatOrder = seatEatOrderMapper.selectById(orderId);
                if (seatEatOrder.getOrderState()==StateDate.SEAT_ORDER_STATE_NOPAY){
                    //修改订单状态，修改支付时间
                    seatEatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_PAY);
                    seatEatOrder.setPayTime(payTime);
                    seatEatOrderMapper.updateById(seatEatOrder);
                }
                //生成订单日志数据
                UserDto data = userServiceClient.findUserById(seatEatOrder.getUserId()).getData();
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderType(seatEatOrder.getOrderType());
                orderLog.setOrderNum(seatEatOrder.getOrderNum());
                orderLog.setLogTime(LocalDateTime.now().toString());
                orderLog.setLogUser(data.getUserName());
                orderLog.setLogDesc(data.getUserName()+"支付完成堂食订单");
                orderLogMapper.insert(orderLog);
            }else{
                //外送订单
                //用户已支付，判断订单是否未支付
                SendOrder seatEatOrder = sendOrderMapper.selectById(orderId);
                if (seatEatOrder.getOrderState()==StateDate.SEND_ORDER_STATE_NOPAY) {
                    //修改订单状态，修改支付时间
                    seatEatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_PAY);
                    seatEatOrder.setPayTime(payTime);
                    sendOrderMapper.updateById(seatEatOrder);
                }
                //生成订单日志数据
                UserDto data = userServiceClient.findUserById(seatEatOrder.getUserId()).getData();
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderType(seatEatOrder.getOrderType());
                orderLog.setOrderNum(seatEatOrder.getOrderNum());
                orderLog.setLogTime(LocalDateTime.now().toString());
                orderLog.setLogUser(data.getUserName());
                orderLog.setLogDesc(data.getUserName()+"支付完成外送订单");
                orderLogMapper.insert(orderLog);
            }

        }
    }

    @Override
    public Object userGetFood(String orderNum, Integer userId,Integer orderType) {
        //订单类型只能是0或1
        if (orderType!=0&&orderType!=1){
            throw new SeatOrderException(500,"订单类型错误");
        }
        if (orderType==0){
            //堂食订单
            //判断订单编号是否存在且是否是用户的
            SeatEatOrder seatEatOrder = seatEatOrderMapper.selectById(orderNum);
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //订单状态必须要是商家已出餐，用户才能进行取餐操作
            if (seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_HAS_FOOD){
                throw new SeatOrderException(500,"订单状态不能取餐");
            }
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(orderType);
            orderLog.setOrderNum(orderNum);
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"获取堂食订单");
            orderLogMapper.insert(orderLog);
            //可以进行取餐
            seatEatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_GET_FOOD);
            seatEatOrder.setGetTime(LocalDateTime.now().toString());
            seatEatOrderMapper.updateById(seatEatOrder);
            SeatOrderDto dto = getOrderDto(seatEatOrder);
            return dto;
        }else{
            //外送订单
            //判断订单编号是否存在且是否是用户的
            SendOrder seatEatOrder = sendOrderMapper.selectById(orderNum);
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //订单状态必须要是商家已出餐，用户才能进行取餐操作
            if (seatEatOrder.getOrderState()!=StateDate.SEND_ORDER_STATE_HAS_FOOD){
                throw new SeatOrderException(500,"订单状态不能取餐");
            }
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(orderType);
            orderLog.setOrderNum(orderNum);
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"收到外送订单");
            orderLogMapper.insert(orderLog);
            //可以进行取餐
            seatEatOrder.setOrderState(StateDate.SEND_ORDER_STATE_GET_FOOD);
            seatEatOrder.setGetTime(LocalDateTime.now().toString());
            sendOrderMapper.updateById(seatEatOrder);
            SendOrderGetDto dto = BeanUtil.toBean(seatEatOrder, SendOrderGetDto.class);
            List<OrderDetail> details = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("order_num", orderNum));
            dto.setCanteenName(details.get(0).getCanteenName());
            dto.setStoreName(details.get(0).getStoreName());
            dto.setFoodOrderList(BeanUtil.copyToList(details, FoodOrderDto.class));
            dto.setOrderStateName("已收到");
            return dto;
        }

    }

    @Override
    public Object refund(OrderRefundParam param, Integer userId) {
        //订单类型只能是0或1
        if (param.getOrderType()!=0&&param.getOrderType()!=1){
            throw new SeatOrderException(500,"订单类型错误");
        }
        if (param.getOrderType()==0){
            //判断订单编号是否存在且是否是用户的
            SeatEatOrder seatEatOrder = seatEatOrderMapper.selectById(param.getOrderNum());
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //用户支付后取餐之前可以进行退款
            if (seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_PAY
                    &&seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_ACCEPT
                    &&seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_HAS_FOOD){
                throw new SeatOrderException(500,"订单状态不能退款");
            }
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(param.getOrderType());
            orderLog.setOrderNum(param.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"发起堂食订单退款");
            orderLogMapper.insert(orderLog);
            //可以提交退款申请
            seatEatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_REFUND_WAIT_STORE);
            seatEatOrder.setRefundReason(param.getRefundReason());
            seatEatOrderMapper.updateById(seatEatOrder);
            SeatOrderDto dto = getOrderDto(seatEatOrder);
            return dto;
        }else{
            //外送订单
            //判断订单编号是否存在且是否是用户的
            SendOrder seatEatOrder = sendOrderMapper.selectById(param.getOrderNum());
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //用户支付后取餐之前可以进行退款
            if (seatEatOrder.getOrderState()!=StateDate.SEND_ORDER_STATE_PAY
                    &&seatEatOrder.getOrderState()!=StateDate.SEND_ORDER_STATE_ACCEPT
                    &&seatEatOrder.getOrderState()!=StateDate.SEND_ORDER_STATE_HAS_FOOD){
                throw new SeatOrderException(500,"订单状态不能退款");
            }
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(param.getOrderType());
            orderLog.setOrderNum(param.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"发起外送订单退款");
            orderLogMapper.insert(orderLog);
            //可以提交退款申请
            seatEatOrder.setOrderState(StateDate.SEND_ORDER_STATE_REFUND_WAIT_STORE);
            seatEatOrder.setRefundReason(param.getRefundReason());
            sendOrderMapper.updateById(seatEatOrder);
            SendOrderRefundDto dto = BeanUtil.toBean(seatEatOrder,SendOrderRefundDto.class);
            List<OrderDetail> details = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("order_num", param.getOrderNum()));
            dto.setCanteenName(details.get(0).getCanteenName());
            dto.setStoreName(details.get(0).getStoreName());
            dto.setFoodOrderList(BeanUtil.copyToList(details, FoodOrderDto.class));
            dto.setOrderStateName("退款待商家审核");
            return dto;
        }

    }

    @Override
    public Object applyPlatform(ApplyOrderParam param, Integer userId) {
        //订单类型只能是0或1
        if (param.getOrderType()!=0&&param.getOrderType()!=1){
            throw new SeatOrderException(500,"订单类型错误");
        }
        //用户申请平台介入
        //判断订单编号是否存在且是否是用户的
        if (param.getOrderType()==0){
            //堂食订单
            SeatEatOrder seatEatOrder = seatEatOrderMapper.selectById(param.getOrderNum());
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //还是用户可以退款的那几种情况可以进行申请平台介入
            if (seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_PAY
                    &&seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_ACCEPT
                    &&seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_HAS_FOOD){
                throw new SeatOrderException(500,"订单状态不能申请平台介入");
            }
            //可以申请平台介入
            seatEatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_REFUND_WAIT_PLATFORM);
            seatEatOrderMapper.updateById(seatEatOrder);
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(param.getOrderType());
            orderLog.setOrderNum(param.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"申请堂食订单退款平台介入");
            orderLogMapper.insert(orderLog);
            return getOrderDto(seatEatOrder);
        }else{
            //外送订单
            SendOrder seatEatOrder = sendOrderMapper.selectById(param.getOrderNum());
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //还是用户可以退款的那几种情况可以进行申请平台介入
            if (seatEatOrder.getOrderState()!=StateDate.SEND_ORDER_STATE_PAY
                    &&seatEatOrder.getOrderState()!=StateDate.SEND_ORDER_STATE_ACCEPT
                    &&seatEatOrder.getOrderState()!=StateDate.SEND_ORDER_STATE_HAS_FOOD){
                throw new SeatOrderException(500,"订单状态不能申请平台介入");
            }
            //可以申请平台介入
            seatEatOrder.setOrderState(StateDate.SEND_ORDER_STATE_REFUND_WAIT_PLATFORM);
            sendOrderMapper.updateById(seatEatOrder);
            SendOrderRefundDto dto = BeanUtil.toBean(seatEatOrder,SendOrderRefundDto.class);
            List<OrderDetail> details = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("order_num", param.getOrderNum()));
            dto.setCanteenName(details.get(0).getCanteenName());
            dto.setStoreName(details.get(0).getStoreName());
            dto.setFoodOrderList(BeanUtil.copyToList(details, FoodOrderDto.class));
            dto.setOrderStateName("退款待平台审核");
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(param.getOrderType());
            orderLog.setOrderNum(param.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"发起外送订单退款");
            orderLogMapper.insert(orderLog);
            return dto;
        }

    }

    @Override
    public Object onceMoreOrder(OrdernumTypeParam param, Integer userId) {
        //订单类型只能是0或1
        if (param.getOrderType()!=0&&param.getOrderType()!=1){
            throw new SeatOrderException(500,"订单类型错误");
        }
        if (param.getOrderType()==0){
            //堂食订单
            //判断订单编号是否存在且是否是用户的
            SeatEatOrder seatEatOrder = seatEatOrderMapper.selectById(param.getOrderNum());
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //已完成/取消/退款的订单才可以进行再来一份
            List<Integer> list = Arrays.asList(1, 3, 6, 7, 8);
            if (!list.contains(seatEatOrder.getOrderState())){
                throw new SeatOrderException(500,"订单状态不能再来一份");
            }
            //可以再来一份，组装生成订单的参数，调用方法
            SeatOrderParam param1 = new SeatOrderParam();
            List<OrderDetail> details = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("order_num", param.getOrderNum()));
            param1.setOrderType(param.getOrderType());
            param1.setRemarks(seatEatOrder.getRemarks());
            param1.setGetUser(seatEatOrder.getGetUser());
            param1.setFoodDtoList(BeanUtil.copyToList(details,FoodOrderForm.class));
            param1.setStoreId(details.get(0).getStoreId());
            param1.setCanteenId(details.get(0).getCanteenId());
            SeatOrderDto dto = ((SeatOrderDto) createOrder(param1, userId));
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(param.getOrderType());
            orderLog.setOrderNum(param.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"再来一次堂食订单");
            orderLogMapper.insert(orderLog);
            return dto;
        }else{
            //外送订单
            //判断订单编号是否存在且是否是用户的
            SendOrder seatEatOrder = sendOrderMapper.selectById(param.getOrderNum());
            if (seatEatOrder==null) throw new SeatOrderException(500,"订单编号不存在");
            if (seatEatOrder.getUserId()!=userId) throw new SeatOrderException(500,"订单不是登录用户的");
            //已完成/取消/退款的订单才可以进行再来一份
            List<Integer> list = Arrays.asList(1, 3, 6, 7, 8);
            if (!list.contains(seatEatOrder.getOrderState())){
                throw new SeatOrderException(500,"订单状态不能再来一份");
            }
            //可以再来一份，组装生成订单的参数，调用方法
            SeatOrderParam param1 = new SeatOrderParam();
            List<OrderDetail> details = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("order_num", param.getOrderNum()));
            param1.setRemarks(seatEatOrder.getOrderRemark());
            param1.setOrderType(param.getOrderType());
            param1.setFoodDtoList(BeanUtil.copyToList(details,FoodOrderForm.class));
            param1.setStoreId(details.get(0).getStoreId());
            param1.setCanteenId(details.get(0).getCanteenId());
            param1.setAddressInfo(seatEatOrder.getAddressInfo());
            param1.setAddressName(seatEatOrder.getAddressName());
            param1.setAddressPhone(seatEatOrder.getAddressPhone());
            SendOrderDto dto = ((SendOrderDto) createOrder(param1, userId));
            //生成订单日志数据
            UserDto data = userServiceClient.findUserById(userId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(param.getOrderType());
            orderLog.setOrderNum(param.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getUserName());
            orderLog.setLogDesc(data.getUserName()+"再来一次外送订单");
            orderLogMapper.insert(orderLog);
            return dto;
        }

    }

    @Override
    public PageInfo<SeatOrderDto> storeFindOrder(StoreFindOrderForm form, Integer storeId) {
        //商家传入的订单类型要正确
        if (form.getOrderType()!=0&&form.getOrderType()!=1){
            throw new SeatOrderException(500,"订单类型错误");
        }
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id",storeId);
        if (form.getFoodName()!=null&&form.getFoodName()!=""){
            wrapper.like("food_name",form.getFoodName());
        }
        if (form.getOrderType()!=null){
            wrapper.eq("order_type",form.getOrderType());
        }
        List<OrderDetail> details = orderDetailMapper.selectList(wrapper);
        //一个订单可能有多个订单详情
        HashMap<String, List<OrderDetail>> hashMap = new HashMap<>();
        for (OrderDetail detail : details) {
            if (hashMap.containsKey(detail.getOrderNum())){
                List<OrderDetail> list = hashMap.get(detail.getOrderNum());
                list.add(detail);
            }else{
                ArrayList<OrderDetail> list = new ArrayList<>();
                list.add(detail);
                hashMap.put(detail.getOrderNum(),list);
            }
        }
        Set<String> orderNums = details.stream().map(OrderDetail::getOrderNum).collect(Collectors.toSet());
        if (form.getOrderType()==0){
            //查询堂食类型
            //分页查询订单数据
            Page<SeatEatOrder> page = new Page<>(form.getPageNum() == null ? 1 : form.getPageNum(), form.getPageSize() == null ? 5 : form.getPageSize());
            QueryWrapper<SeatEatOrder> queryWrapper = new QueryWrapper<>();
            if (orderNums.size()>0){
                queryWrapper.in("order_num",orderNums);
            }
            seatEatOrderMapper.selectPage(page,queryWrapper);
            PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
            List<SeatOrderDto> dtos = BeanUtil.copyToList(page.getRecords(), SeatOrderDto.class);
            for (SeatOrderDto dto : dtos) {
                dto.setCanteenName(hashMap.get(dto.getOrderNum()).get(0).getCanteenName());
                dto.setStoreName(hashMap.get(dto.getOrderNum()).get(0).getStoreName());
                dto.setFoodOrderList(BeanUtil.copyToList(hashMap.get(dto.getOrderNum()),FoodOrderDto.class));
                dto.setOrderStateName(dto.getOrderState()==0?"待支付":
                        dto.getOrderState()==1?"已取消":
                        dto.getOrderState()==2?"待接单":
                        dto.getOrderState()==3?"商家未接单":
                        dto.getOrderState()==4?"已接单":
                        dto.getOrderState()==5?"已出餐":
                        dto.getOrderState()==6?"已取餐":
                        dto.getOrderState()==7?"已退款":
                        dto.getOrderState()==8?"已评价":
                        dto.getOrderState()==9?"退款待商家审核":"退款待平台审核");
            }
            pageInfo.setList(dtos);
            pageInfo.setPages(page.getPages());
            return pageInfo;
        }else{
            //查询外送类型
            //分页查询订单数据
            Page<SendOrder> page = new Page<>(form.getPageNum() == null ? 1 : form.getPageNum(), form.getPageSize() == null ? 5 : form.getPageSize());
            QueryWrapper<SendOrder> queryWrapper = new QueryWrapper<>();
            if (orderNums.size()>0){
                queryWrapper.in("order_num",orderNums);
            }
            sendOrderMapper.selectPage(page,queryWrapper);
            PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
            List<SendOrderFindDto> dtos = BeanUtil.copyToList(page.getRecords(), SendOrderFindDto.class);
            for (SendOrderFindDto dto : dtos) {
                dto.setCanteenName(hashMap.get(dto.getOrderNum()).get(0).getCanteenName());
                dto.setStoreName(hashMap.get(dto.getOrderNum()).get(0).getStoreName());
                dto.setFoodOrderList(BeanUtil.copyToList(hashMap.get(dto.getOrderNum()),FoodOrderDto.class));
                dto.setOrderStateName(dto.getOrderState()==0?"待支付":
                        dto.getOrderState()==1?"已取消":
                        dto.getOrderState()==2?"待接单":
                        dto.getOrderState()==3?"商家未接单":
                        dto.getOrderState()==4?"已接单":
                        dto.getOrderState()==5?"已出餐":
                        dto.getOrderState()==6?"已取餐":
                        dto.getOrderState()==7?"已退款":
                        dto.getOrderState()==8?"已评价":
                        dto.getOrderState()==9?"退款待商家审核":"退款待平台审核");
            }
            pageInfo.setList(dtos);
            pageInfo.setPages(page.getPages());
            return pageInfo;
        }

    }

    @Override
    public Object acceptOrder(OrdernumTypeForm form, Integer storeId) {
        //订单类型要正确
        if (form.getOrderType()!=0&&form.getOrderType()!=1){
            throw new SeatOrderException(500,"订单类型错误");
        }
        //订单是否存在及是否是该商家的
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",form.getOrderNum());
        wrapper.eq("store_id",storeId);
        List<OrderDetail> details = orderDetailMapper.selectList(wrapper);
        if (details.size()==0) throw new SeatOrderException(500,"订单不存在或不属于该商家");
        if (form.getOrderType()==0){
            //堂食订单
            //订单为已支付状态商家才能订单
            SeatEatOrder eatOrder = seatEatOrderMapper.selectById(form.getOrderNum());
            if (eatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_PAY){
                throw new SeatOrderException(500,"订单状态错误");
            }
            //商家可以进行接受订单
            eatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_ACCEPT);
            eatOrder.setStoreAcceptTime(LocalDateTime.now().toString());
            seatEatOrderMapper.updateById(eatOrder);
            //生成订单日志数据
            StoreDto data = storeServiceClient.findStoreById(storeId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(form.getOrderType());
            orderLog.setOrderNum(form.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getStoreName());
            orderLog.setLogDesc(data.getStoreName()+"接收堂食订单");
            orderLogMapper.insert(orderLog);

            return getOrderDto(eatOrder);
        }else{
            //外送订单
            //订单为已支付状态商家才能订单
            SendOrder eatOrder = sendOrderMapper.selectById(form.getOrderNum());
            if (eatOrder.getOrderState()!=StateDate.SEND_ORDER_STATE_PAY){
                throw new SeatOrderException(500,"订单状态错误");
            }
            //商家可以进行接受订单
            eatOrder.setOrderState(StateDate.SEND_ORDER_STATE_ACCEPT);
            eatOrder.setStoreAcceptTime(LocalDateTime.now().toString());
            sendOrderMapper.updateById(eatOrder);
            SendOrderAcceptDto dto = BeanUtil.toBean(eatOrder, SendOrderAcceptDto.class);
            dto.setCanteenName(details.get(0).getCanteenName());
            dto.setStoreName(details.get(0).getStoreName());
            dto.setFoodOrderList(BeanUtil.copyToList(details,FoodOrderDto.class));
            dto.setOrderStateName("商家已接单");
            //生成订单日志数据
            StoreDto data = storeServiceClient.findStoreById(storeId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(form.getOrderType());
            orderLog.setOrderNum(form.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getStoreName());
            orderLog.setLogDesc(data.getStoreName()+"接收外送订单");
            orderLogMapper.insert(orderLog);
            return dto;
        }

    }

    @Override
    public Object storeRefuseStore(OrderRefuseParam param, Integer storeId) {
        //订单类型只能是0或1
        if (param.getOrderType()!=0&&param.getOrderType()!=1){
            throw new SeatOrderException(500,"订单类型错误");
        }
        //商家拒绝订单：订单是否存在、是否是该商家的、订单状态是否正确
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",param.getOrderNum());
        List<OrderDetail> details = orderDetailMapper.selectList(wrapper);
        if (details.size()==0) throw new SeatOrderException(500,"订单不存在");
        if (details.get(0).getStoreId()!=storeId) throw new SeatOrderException(500,"订单不属于该商家");
        if (param.getOrderType()==0){
            //堂食订单
            //用户支付后商家才能进行拒绝操作
            SeatEatOrder seatEatOrder = seatEatOrderMapper.selectById(param.getOrderNum());
            if (seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_PAY){
                throw new SeatOrderException(500,"订单状态错误");
            }
            //可以进行拒绝
            seatEatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_STORE_CANCEL);
            seatEatOrder.setStoreRefuseReason(param.getStoreRefuseReason());
            seatEatOrder.setCancelTime(LocalDateTime.now().toString());
            seatEatOrderMapper.updateById(seatEatOrder);
            //返回数据
            SeatOrderDto orderDto = getOrderDto(seatEatOrder);
            RefuseOrderDto dto = BeanUtil.toBean(orderDto, RefuseOrderDto.class);
            dto.setStoreRefuseReason(param.getStoreRefuseReason());
            dto.setCancelTime(seatEatOrder.getCancelTime());
            //生成订单日志数据
            StoreDto data = storeServiceClient.findStoreById(storeId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(param.getOrderType());
            orderLog.setOrderNum(param.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getStoreName());
            orderLog.setLogDesc(data.getStoreName()+"拒绝堂食订单");
            orderLogMapper.insert(orderLog);
            return dto;
        }else{
            //外送订单
            //用户支付后商家才能进行拒绝操作
            SendOrder seatEatOrder = sendOrderMapper.selectById(param.getOrderNum());
            if (seatEatOrder.getOrderState()!=StateDate.SEND_ORDER_STATE_PAY){
                throw new SeatOrderException(500,"订单状态错误");
            }
            //可以进行拒绝
            seatEatOrder.setOrderState(StateDate.SEND_ORDER_STATE_STORE_CANCEL);
            seatEatOrder.setSotreRefuseReason(param.getStoreRefuseReason());
            seatEatOrder.setCancelTime(LocalDateTime.now().toString());
            sendOrderMapper.updateById(seatEatOrder);
            //返回数据
            SendOrderRefuseDto dto = BeanUtil.toBean(seatEatOrder, SendOrderRefuseDto.class);
            dto.setCanteenName(details.get(0).getCanteenName());
            dto.setStoreName(details.get(0).getStoreName());
            dto.setFoodOrderList(BeanUtil.copyToList(details,FoodOrderDto.class));
            dto.setOrderStateName("商家取消");
            //生成订单日志数据
            StoreDto data = storeServiceClient.findStoreById(storeId).getData();
            OrderLog orderLog = new OrderLog();
            orderLog.setOrderType(param.getOrderType());
            orderLog.setOrderNum(param.getOrderNum());
            orderLog.setLogTime(LocalDateTime.now().toString());
            orderLog.setLogUser(data.getStoreName());
            orderLog.setLogDesc(data.getStoreName()+"拒绝外送订单");
            orderLogMapper.insert(orderLog);
            return dto;
        }

    }

    @Override
    public OutOrderDto storeOutOrder(String orderNum, Integer storeId) {
        //商家出餐订单：订单是否存在、是否是该商家的、订单状态是否正确
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",orderNum);
        List<OrderDetail> details = orderDetailMapper.selectList(wrapper);
        if (details.size()==0) throw new SeatOrderException(500,"订单不存在");
        if (details.get(0).getStoreId()!=storeId) throw new SeatOrderException(500,"订单不属于该商家");
        //商家接单后才能进行出餐
        SeatEatOrder seatEatOrder = seatEatOrderMapper.selectById(orderNum);
        if (seatEatOrder.getOrderState()!=StateDate.SEAT_ORDER_STATE_ACCEPT){
            throw new SeatOrderException(500,"订单状态错误");
        }
        //商家可以进行出餐
        seatEatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_HAS_FOOD);
        seatEatOrder.setStoreOutTime(LocalDateTime.now().toString());
        seatEatOrderMapper.updateById(seatEatOrder);
        //返回数据
        SeatOrderDto orderDto = getOrderDto(seatEatOrder);
        OutOrderDto dto = BeanUtil.toBean(orderDto, OutOrderDto.class);
        dto.setStoreOutTime(seatEatOrder.getStoreOutTime());
        //生成订单日志数据
        StoreDto data = storeServiceClient.findStoreById(storeId).getData();
        OrderLog orderLog = new OrderLog();
        orderLog.setOrderType(details.get(0).getOrderType());
        orderLog.setOrderNum(orderNum);
        orderLog.setLogTime(LocalDateTime.now().toString());
        orderLog.setLogUser(data.getStoreName());
        orderLog.setLogDesc(data.getStoreName()+"出餐完成");
        orderLogMapper.insert(orderLog);
        return dto;
    }

    @Override
    public Object storeCheckRefund(StoreCheckParam param, Integer storeId) {
        //订单类型只能是0或1
        if (param.getOrderType()!=0&&param.getOrderType()!=1){
            throw new SeatOrderException(500,"订单类型错误");
        }
        //商家审核退款：订单是否存在、是否是该商家的、订单状态是否正确
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",param.getOrderNum());
        List<OrderDetail> details = orderDetailMapper.selectList(wrapper);
        if (details.size()==0) throw new SeatOrderException(500,"订单不存在");
        if (details.get(0).getStoreId()!=storeId) throw new SeatOrderException(500,"订单不属于该商家");
        //传递进来的是否通过参数只能是0或1
        if (param.getCheckState()!=0&&param.getCheckState()!=1){
            throw new SeatOrderException(500,"审核状态错误");
        }
        if (param.getOrderType()==0){
            //堂食订单退款
            //订单状态为退款待商家审核才能进行审核操作
            SeatEatOrder order = seatEatOrderMapper.selectById(param.getOrderNum());
            if (order.getOrderState()!=StateDate.SEAT_ORDER_STATE_REFUND_WAIT_STORE){
                throw new SeatOrderException(500,"订单状态错误");
            }
            //可以进行审核操作
            if (param.getCheckState()==0){
                //通过退款
                order.setOrderState(StateDate.SEAT_ORDER_STATE_REFUND);
                order.setStoreCheckInfo("审核通过退款");
                //生成订单日志数据
                StoreDto data = storeServiceClient.findStoreById(storeId).getData();
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderType(param.getOrderType());
                orderLog.setOrderNum(param.getOrderNum());
                orderLog.setLogTime(LocalDateTime.now().toString());
                orderLog.setLogUser(data.getStoreName());
                orderLog.setLogDesc(data.getStoreName()+"商家同意退款堂食订单");
                orderLogMapper.insert(orderLog);
            }else{
                //拒绝退款
                order.setOrderState(StateDate.SEAT_ORDER_STATE_HAS_FOOD);
                order.setStoreCheckInfo("审核退款不通过");
                //生成订单日志数据
                StoreDto data = storeServiceClient.findStoreById(storeId).getData();
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderType(param.getOrderType());
                orderLog.setOrderNum(param.getOrderNum());
                orderLog.setLogTime(LocalDateTime.now().toString());
                orderLog.setLogUser(data.getStoreName());
                orderLog.setLogDesc(data.getStoreName()+"商家拒绝退款堂食订单");
                orderLogMapper.insert(orderLog);
            }
            seatEatOrderMapper.updateById(order);
            //返回数据
            SeatOrderDto orderDto = getOrderDto(order);
            StoreCheckOrderDto dto = BeanUtil.toBean(orderDto, StoreCheckOrderDto.class);
            dto.setRefundReason(order.getRefundReason());
            dto.setStoreCheckInfo(order.getStoreCheckInfo());
            return dto;
        }else{
            //外送订单退款
            //订单状态为退款待商家审核才能进行审核操作
            SendOrder order = sendOrderMapper.selectById(param.getOrderNum());
            if (order.getOrderState()!=StateDate.SEND_ORDER_STATE_REFUND_WAIT_STORE){
                throw new SeatOrderException(500,"订单状态错误");
            }
            //可以进行审核操作
            if (param.getCheckState()==0){
                //通过退款
                order.setOrderState(StateDate.SEND_ORDER_STATE_REFUND);
                order.setStoreCheckInfo("审核通过退款");
                //生成订单日志数据
                StoreDto data = storeServiceClient.findStoreById(storeId).getData();
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderType(param.getOrderType());
                orderLog.setOrderNum(param.getOrderNum());
                orderLog.setLogTime(LocalDateTime.now().toString());
                orderLog.setLogUser(data.getStoreName());
                orderLog.setLogDesc(data.getStoreName()+"商家同意退款外送订单");
                orderLogMapper.insert(orderLog);
            }else{
                //拒绝退款
                order.setOrderState(StateDate.SEND_ORDER_STATE_HAS_FOOD);
                order.setStoreCheckInfo("审核退款不通过");
                //生成订单日志数据
                StoreDto data = storeServiceClient.findStoreById(storeId).getData();
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderType(param.getOrderType());
                orderLog.setOrderNum(param.getOrderNum());
                orderLog.setLogTime(LocalDateTime.now().toString());
                orderLog.setLogUser(data.getStoreName());
                orderLog.setLogDesc(data.getStoreName()+"商家拒绝退款外送订单");
                orderLogMapper.insert(orderLog);
            }
            sendOrderMapper.updateById(order);
            //返回数据
            SendOrderRefundDto dto = BeanUtil.toBean(order, SendOrderRefundDto.class);
            dto.setCanteenName(details.get(0).getCanteenName());
            dto.setStoreName(details.get(0).getStoreName());
            dto.setFoodOrderList(BeanUtil.copyToList(details,FoodOrderDto.class));
            dto.setOrderStateName("已退款");
            return dto;
        }

    }

    @Override
    public PageInfo<PlatformFindOrderDto> platformFindOrder(PlatformFindParam param) {
        //订单类型只能是0或1
        if (param.getOrderType()!=0&&param.getOrderType()!=1){
            throw new SeatOrderException(500,"订单类型错误");
        }
        if (param.getOrderType()==0) {
            //堂食订单
            Page<SeatEatOrder> page = new Page<>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 5 : param.getPageSize());
            //先分页查询订单表数据
            QueryWrapper<SeatEatOrder> wrapper = new QueryWrapper<>();
            if (param.getOrderNum() != null && param.getOrderNum() != "") {
                wrapper.like("order_num", param.getOrderNum());
            }
            if (param.getOrderType() != null) {
                wrapper.eq("order_type", param.getOrderType());
            }
            if (param.getOrderState() != null) {
                wrapper.eq("order_state", param.getOrderState());
            }

            seatEatOrderMapper.selectPage(page, wrapper);
            List<SeatEatOrder> records = page.getRecords();
            List<String> orderNums = records.stream().map(SeatEatOrder::getOrderNum).collect(Collectors.toList());

            //再条件查询订单详情表数据
            QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("order_num", orderNums);
            if (param.getCanteenName() != null && param.getCanteenName() != "") {
                queryWrapper.like("canteen_name", param.getCanteenName());
            }
            if (param.getStoreName() != null && param.getStoreName() != "") {
                queryWrapper.like("store_name", param.getStoreName());
            }
            List<OrderDetail> details = orderDetailMapper.selectList(queryWrapper);
            HashMap<String, List<OrderDetail>> map = new HashMap<>();
            for (OrderDetail detail : details) {
                if (map.containsKey(detail.getOrderNum())) {
                    List<OrderDetail> orderDetails = map.get(detail.getOrderNum());
                    orderDetails.add(detail);
                    map.put(detail.getOrderNum(), orderDetails);
                } else {
                    ArrayList<OrderDetail> orderDetails = new ArrayList<>();
                    orderDetails.add(detail);
                    map.put(detail.getOrderNum(), orderDetails);
                }
            }
            //组装返回的pageInfo
            PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
            List<PlatformFindOrderDto> list = BeanUtil.copyToList(records, PlatformFindOrderDto.class);
            for (PlatformFindOrderDto dto : list) {
                dto.setOrderStateName(dto.getOrderState() == 0 ? "待支付" :
                        dto.getOrderState() == 1 ? "已取消" :
                                dto.getOrderState() == 2 ? "待接单" :
                                        dto.getOrderState() == 3 ? "商家未接单" :
                                                dto.getOrderState() == 4 ? "已接单" :
                                                        dto.getOrderState() == 5 ? "已出餐" :
                                                                dto.getOrderState() == 6 ? "已取餐" :
                                                                        dto.getOrderState() == 7 ? "已退款" :
                                                                                dto.getOrderState() == 8 ? "已评价" :
                                                                                        dto.getOrderState() == 9 ? "退款待商家审核" : "退款待平台审核");
                dto.setCanteenName(map.get(dto.getOrderNum()).get(0).getCanteenName());
                dto.setStoreName(map.get(dto.getOrderNum()).get(0).getStoreName());
                dto.setFoodOrderList(BeanUtil.copyToList(map.get(dto.getOrderNum()), FoodOrderDto.class));
                dto.setTypeName(dto.getOrderType() == 0 ? "堂食" : "外卖");
            }
            pageInfo.setList(list);
            pageInfo.setPages(page.getPages());
            return pageInfo;
        }else{
            //外送订单
            Page<SendOrder> page = new Page<>(param.getPageNum()==null?1:param.getPageNum(),param.getPageSize()==null?5:param.getPageSize());
            //先分页查询订单表数据
            QueryWrapper<SendOrder> wrapper = new QueryWrapper<>();
            if (param.getOrderNum()!=null&&param.getOrderNum()!=""){
                wrapper.like("order_num",param.getOrderNum());
            }
            if (param.getOrderType()!=null){
                wrapper.eq("order_type",param.getOrderType());
            }
            if (param.getOrderState()!=null){
                wrapper.eq("order_state",param.getOrderState());
            }

            sendOrderMapper.selectPage(page,wrapper);
            List<SendOrder> records = page.getRecords();
            List<String> orderNums = records.stream().map(SendOrder::getOrderNum).collect(Collectors.toList());

            //再条件查询订单详情表数据
            QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("order_num",orderNums);
            if (param.getCanteenName()!=null&&param.getCanteenName()!=""){
                queryWrapper.like("canteen_name",param.getCanteenName());
            }
            if (param.getStoreName()!=null&&param.getStoreName()!=""){
                queryWrapper.like("store_name",param.getStoreName());
            }
            List<OrderDetail> details = orderDetailMapper.selectList(queryWrapper);
            HashMap<String, List<OrderDetail>> map = new HashMap<>();
            for (OrderDetail detail : details) {
                if (map.containsKey(detail.getOrderNum())){
                    List<OrderDetail> orderDetails = map.get(detail.getOrderNum());
                    orderDetails.add(detail);
                }else{
                    ArrayList<OrderDetail> orderDetails = new ArrayList<>();
                    orderDetails.add(detail);
                    map.put(detail.getOrderNum(),orderDetails);
                }
            }
            //组装返回的pageInfo
            PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
            List<SendOrderFindDto> list = BeanUtil.copyToList(records, SendOrderFindDto.class);
            for (SendOrderFindDto dto : list) {
                dto.setOrderStateName(dto.getOrderState()==0?"待支付":
                        dto.getOrderState()==1?"已取消":
                        dto.getOrderState()==2?"待接单":
                        dto.getOrderState()==3?"商家未接单":
                        dto.getOrderState()==4?"已接单":
                        dto.getOrderState()==5?"已出餐":
                        dto.getOrderState()==6?"已取餐":
                        dto.getOrderState()==7?"已退款":
                        dto.getOrderState()==8?"已评价":
                        dto.getOrderState()==9?"退款待商家审核":"退款待平台审核");
                dto.setCanteenName(map.get(dto.getOrderNum()).get(0).getCanteenName());
                dto.setStoreName(map.get(dto.getOrderNum()).get(0).getStoreName());
                dto.setFoodOrderList(BeanUtil.copyToList(map.get(dto.getOrderNum()),FoodOrderDto.class));
                dto.setTypeName(dto.getOrderType()==0?"堂食":"外卖");
            }
            pageInfo.setList(list);
            pageInfo.setPages(page.getPages());
            return pageInfo;
        }

    }

    @Override
    public Object platformCheckOrder(PlatformCheckOrderForm form, int checkState) throws AlipayApiException {
        //订单类型只能是0或1
        if (form.getOrderType()!=0&&form.getOrderType()!=1){
            throw new SeatOrderException(500,"订单类型错误");
        }
        if (form.getOrderType()==0){
            //堂食订单
            //查询订单是否存在
            SeatEatOrder order = seatEatOrderMapper.selectById(form.getOrderNum());
            if (order==null) throw new SeatOrderException(500,"订单不存在");
            //订单状态必须是待平台审核
            if (order.getOrderState()!=StateDate.SEAT_ORDER_STATE_REFUND_WAIT_PLATFORM){
                throw new SeatOrderException(500,"订单状态错误");
            }
            //判断传递进来的审核状态只能是0或1
            if (checkState!=0&&checkState!=1){
                throw new SeatOrderException(500,"审核状态错误");
            }
            if (checkState == 0) {
                //审核通过
                order.setOrderState(StateDate.SEAT_ORDER_STATE_REFUND);
                order.setPlatformCheckInfo("平台审核退款通过");
                seatEatOrderMapper.updateById(order);
                //调用退款方法
                boolean refund = alipayUtil.refund(order);
                if (!refund) throw new SeatOrderException(500,"退款失败");
                //生成订单日志数据
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderType(form.getOrderType());
                orderLog.setOrderNum(form.getOrderNum());
                orderLog.setLogTime(LocalDateTime.now().toString());
                orderLog.setLogUser("平台");
                orderLog.setLogDesc("平台同意退款堂食订单");
                orderLogMapper.insert(orderLog);
            }else{
                //审核不通过
                order.setOrderState(StateDate.SEAT_ORDER_STATE_HAS_FOOD);
                order.setPlatformCheckInfo("平台审核退款不通过");
                seatEatOrderMapper.updateById(order);
                //生成订单日志数据
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderType(form.getOrderType());
                orderLog.setOrderNum(form.getOrderNum());
                orderLog.setLogTime(LocalDateTime.now().toString());
                orderLog.setLogUser("平台");
                orderLog.setLogDesc("平台拒绝退款堂食订单");
                orderLogMapper.insert(orderLog);
            }
            //返回数据
            List<OrderDetail> details = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("order_num", form.getOrderNum()));
            PlatformFindOrderDto dto = BeanUtil.toBean(order, PlatformFindOrderDto.class);
            dto.setOrderStateName(changeOrderStateName(order.getOrderState()));
            dto.setCanteenName(details.get(0).getCanteenName());
            dto.setStoreName(details.get(0).getStoreName());
            dto.setFoodOrderList(BeanUtil.copyToList(details,FoodOrderDto.class));
            dto.setTypeName(dto.getOrderType()==0?"堂食":"外卖");
            return dto;
        }else{
            //外送订单
            //查询订单是否存在
            SendOrder order = sendOrderMapper.selectById(form.getOrderNum());
            if (order==null) throw new SeatOrderException(500,"订单不存在");
            //订单状态必须是待平台审核
            if (order.getOrderState()!=StateDate.SEND_ORDER_STATE_REFUND_WAIT_PLATFORM){
                throw new SeatOrderException(500,"订单状态错误");
            }
            //判断传递进来的审核状态只能是0或1
            if (checkState!=0&&checkState!=1){
                throw new SeatOrderException(500,"审核状态错误");
            }
            if (checkState == 0) {
                //审核通过
                order.setOrderState(StateDate.SEND_ORDER_STATE_REFUND);
                order.setPlatformCheckInfo("平台审核退款通过");
                sendOrderMapper.updateById(order);
                //调用退款方法
                boolean refund = alipayUtil.refund(order);
                if (!refund) throw new SeatOrderException(500,"退款失败");
                //生成订单日志数据
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderType(form.getOrderType());
                orderLog.setOrderNum(form.getOrderNum());
                orderLog.setLogTime(LocalDateTime.now().toString());
                orderLog.setLogUser("平台");
                orderLog.setLogDesc("平台同意退款外送订单");
                orderLogMapper.insert(orderLog);
            }else{
                //审核不通过
                order.setOrderState(StateDate.SEND_ORDER_STATE_HAS_FOOD);
                order.setPlatformCheckInfo("平台审核退款不通过");
                sendOrderMapper.updateById(order);
                //生成订单日志数据
                OrderLog orderLog = new OrderLog();
                orderLog.setOrderType(form.getOrderType());
                orderLog.setOrderNum(form.getOrderNum());
                orderLog.setLogTime(LocalDateTime.now().toString());
                orderLog.setLogUser("平台");
                orderLog.setLogDesc("平台拒绝退款外送订单");
                orderLogMapper.insert(orderLog);
            }
            //返回数据
            List<OrderDetail> details = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("order_num", form.getOrderNum()));
            SendOrderFindDto dto = BeanUtil.toBean(order, SendOrderFindDto.class);
            dto.setOrderStateName(changeOrderStateName(order.getOrderState()));
            dto.setCanteenName(details.get(0).getCanteenName());
            dto.setStoreName(details.get(0).getStoreName());
            dto.setFoodOrderList(BeanUtil.copyToList(details,FoodOrderDto.class));
            dto.setTypeName(dto.getOrderType()==0?"堂食":"外卖");
            return dto;
        }

    }

    //传入订单编号设置订单状态的名字
    private String changeOrderStateName(Integer  orderState){
        return  orderState==0?"待支付":
                orderState==1?"已取消":
                orderState==2?"待接单":
                orderState==3?"商家未接单":
                orderState==4?"已接单":
                orderState==5?"已出餐":
                orderState==6?"已取餐":
                orderState==7?"已退款":
                orderState==8?"已评价":
                orderState==9?"退款待商家审核":"退款待平台审核";
    }

    //传入订单获取订单详情信息
    private SeatOrderDto getOrderDto(SeatEatOrder order){
        SeatOrderDto dto = BeanUtil.toBean(order, SeatOrderDto.class);
        List<OrderDetail> details = orderDetailMapper.selectList(new QueryWrapper<OrderDetail>().eq("order_num", order.getOrderNum()));
        dto.setCanteenName(details.get(0).getCanteenName());
        dto.setStoreName(details.get(0).getStoreName());
        dto.setOrderStateName(order.getOrderState()==0?"待支付":
                order.getOrderState()==1?"已取消":
                order.getOrderState()==2?"待接单":
                order.getOrderState()==3?"商家未接单":
                order.getOrderState()==4?"已接单":
                order.getOrderState()==5?"已出餐":
                order.getOrderState()==6?"已取餐":"已退款");
        dto.setFoodOrderList(BeanUtil.copyToList(details,FoodOrderDto.class));
        return dto;
    }

    public static void main(String[] args) {
//        List<Integer> list = Arrays.asList(1, 2, 3);
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(1,1);
        System.out.println(map.get(1));
        Integer integer = map.get(1);
        integer=2;
        map.put(1,integer);
        System.out.println(map.get(1));
    }


}
